/**
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.elementstemplate;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.ArcGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.Node;
import org.pnml.tools.epnk.pnmlcoremodel.NodeGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.PlaceNode;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.HLAnnotation;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.HlpngdefinitionFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Type;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.MultiSetSort;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.NamedSort;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Sort;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.SortDecl;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.TermsFactory;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.UserSort;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Variable;
import org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.VariableDecl;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractGINet;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractginetFactory;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl.PlaceImpl;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.NodeStructure;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.TreeInterface;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Termination;

/**
 * @author Neri
 * 
 */
public abstract class ChoreographyStructure {

	protected AbstractGINet abstractGinet = AbstractginetFactory.eINSTANCE
			.createAbstractGINet();
	
	protected Page abstractPage;

	protected EList<NamedSort> namedSorts;
	
	protected EList<VariableDecl> variableDecls;
	
	static protected Map<Integer, Float> columns = new HashMap<Integer, Float>();
	
	static protected Map<Integer, Float> rows = new HashMap<Integer, Float>();
	
	protected TreeInterface tree;
	
	public Page getPage(){
		return this.abstractPage;
	}
	
	 // Static constructor
	  static {			
			//set columns
			rows.put(1,(float) 115);
			rows.put(2,(float) 230);
			rows.put(3,(float) 345);
			rows.put(4,(float) 460);
			rows.put(5,(float) 575);
			rows.put(6,(float) 690);
			rows.put(7,(float) 805);
			rows.put(8,(float) 920);
			rows.put(9,(float) 1035);
			
			//set rows
			columns.put(1, (float) 55);
			columns.put(2, (float) 140);
			columns.put(3, (float) 230);
			columns.put(4, (float) 320);
			columns.put(5, (float) 410);
			columns.put(6, (float) 500);
			columns.put(7, (float) 590);
			columns.put(8, (float) 680);
			columns.put(9, (float) 770);
	  }
	
	public ChoreographyStructure(){
		this.abstractPage = AbstractginetFactory.eINSTANCE.createPage();
		
		this.namedSorts = new BasicEList<NamedSort>();
		
		this.variableDecls = new BasicEList<VariableDecl>();
	}

	public Place createPlace(String id,
			int xPosition, int yPosition, String name, float nameX,
			float nameY, VariableDecl typePlace, float typeX, float typeY,
			boolean isInput, String parallelCardinality) {
		Place place = AbstractginetFactory.eINSTANCE.createPlace();

		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;

		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();

		coordinate.setX(40);
		coordinate.setY(40);

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(columns.get(xPosition));
		coordinate1.setY(rows.get(yPosition));

		graphics.setPosition(coordinate1);

		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		nameLabel.setText(name);

		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();

		coordinate2.setX(nameX);
		coordinate2.setY(nameY);

		graphicsName.setOffset(coordinate2);

		Type type = HlpngdefinitionFactory.eINSTANCE.createType();
		
		MultiSetSort multisort = (MultiSetSort) typePlace.getSort();

		type.setStructure(multisort.getBasis().clone());
		type.setText(((UserSort)multisort.getBasis()).getName());
		
		Coordinate coordinate3 = pnmlcoremodelFactory.createCoordinate();

		coordinate3.setX(typeX);
		coordinate3.setY(typeY);

		graphicsName.setOffset(coordinate3);

		type.setGraphics(graphicsName);

		place.setName(nameLabel);

		place.setGraphics(graphics);

		place.setType(type);
		
		place.setId(id);
		
		place.setParallelCardinality(parallelCardinality);
		
		if(isInput){
			place.setInput(isInput);
			place.setOutput(!isInput);
		}else{
			place.setInput(!isInput);
			place.setOutput(isInput);
		}

		return place;
	}
	
	public Place createPlace(String id,
			float xPosition, float yPosition, String name, float nameX,
			float nameY, VariableDecl typePlace, float typeX, float typeY,
			boolean isInput, String parallelCardinality) {
		Place place = AbstractginetFactory.eINSTANCE.createPlace();

		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;

		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();

		coordinate.setX(40);
		coordinate.setY(40);

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(xPosition);
		coordinate1.setY(yPosition);

		graphics.setPosition(coordinate1);

		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		nameLabel.setText(name);

		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();

		coordinate2.setX(nameX);
		coordinate2.setY(nameY);

		graphicsName.setOffset(coordinate2);

		Type type = HlpngdefinitionFactory.eINSTANCE.createType();
		
		MultiSetSort multisort = (MultiSetSort) typePlace.getSort();

		type.setStructure(multisort.getBasis().clone());
		type.setText(((UserSort)multisort.getBasis()).getName());
		
		Coordinate coordinate3 = pnmlcoremodelFactory.createCoordinate();

		coordinate3.setX(typeX);
		coordinate3.setY(typeY);

		graphicsName.setOffset(coordinate3);

		type.setGraphics(graphicsName);

		place.setName(nameLabel);

		place.setGraphics(graphics);

		place.setType(type);
		
		place.setId(id);
		
		place.setParallelCardinality(parallelCardinality);
		
		if(isInput){
			place.setInput(isInput);
			place.setOutput(!isInput);
		}else{
			place.setInput(!isInput);
			place.setOutput(isInput);
		}

		return place;
	}
	
	protected Transition createTransition(String id,
			int xPosition, int yPosition, String name, float nameX,
			float nameY, String parallelCardinality, String sequentialCardinality, Page refPage){
		Transition transition = AbstractginetFactory.eINSTANCE.createTransition();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();

		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		coordinate.setX(nameX);
		coordinate.setY(nameY);
		
		graphicsName.setOffset(coordinate);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);

		coordinate.setX(columns.get(xPosition));
		coordinate.setY(rows.get(yPosition));

		graphics.setPosition(coordinate);
		
		transition.setGraphics(graphics);
		transition.setId(id);		
		transition.setName(nameLabel);
		transition.setParallelCardinality(parallelCardinality);
		transition.setSequentialCardinality(sequentialCardinality);
		transition.setRefPage(refPage);
		
		return transition;
		
	}

	protected Transition createTransition(String id,
			float xPosition, float yPosition, String name, float nameX,
			float nameY, String parallelCardinality, String sequentialCardinality, Page refPage){
		Transition transition = AbstractginetFactory.eINSTANCE.createTransition();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();

		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		coordinate.setX(nameX);
		coordinate.setY(nameY);
		
		graphicsName.setOffset(coordinate);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);

		coordinate.setX(xPosition);
		coordinate.setY(yPosition);

		graphics.setPosition(coordinate);
		
		transition.setGraphics(graphics);
		transition.setId(id);		
		transition.setName(nameLabel);
		transition.setParallelCardinality(parallelCardinality);
		transition.setSequentialCardinality(sequentialCardinality);
		transition.setRefPage(refPage);
		
		return transition;
		
	}
	
	protected Arc createArc(String id,  VariableDecl var, float nameX,
			float nameY, Node source, Node target){
		
		Arc arc = AbstractginetFactory.eINSTANCE.createArc();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(var.getName());
		nameLabel.setGraphics(graphicsName);
		
		HLAnnotation annotation = HlpngdefinitionFactory.eINSTANCE.createHLAnnotation();
		AnnotationGraphics labelPos = PnmlcoremodelFactory.eINSTANCE.createAnnotationGraphics();
		Coordinate offset = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		offset.setX(0);
		offset.setY(30);
		labelPos.setOffset(offset);
		annotation.setGraphics(labelPos);
		annotation.setText(var.getName());
		
		Variable variable = TermsFactory.eINSTANCE.createVariable();
		
		variable.setRefvariable(var);
		annotation.setStructure(variable);


		arc.setId(id);
		arc.setName(nameLabel);
		arc.setSource(source);
		arc.setTarget(target);
		arc.setHlinscription(annotation);
		
		return arc;
		
	}
	
	protected Arc createArc(String id,
			float xPosition, float yPosition, float xPosition1, float yPosition1, VariableDecl var, float nameX,
			float nameY, Node source, Node target){
		
		Arc arc = AbstractginetFactory.eINSTANCE.createArc();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//graphics
		ArcGraphics graphics = pnmlcoremodelFactory.createArcGraphics();

		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(xPosition);
		coordinate1.setY(yPosition);
		
		graphics.getPosition().add(coordinate1);
		
		Coordinate coordinate3 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate3.setX(xPosition);
		coordinate3.setY(yPosition);
		
		graphics.getPosition().add(coordinate3);
		
		Coordinate coordinate4 = pnmlcoremodelFactory.createCoordinate();

		coordinate4.setX(xPosition1);
		coordinate4.setY(yPosition1);
		
		graphics.getPosition().add(coordinate4);
		
		Coordinate coordinate5 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate5.setX(xPosition1);
		coordinate5.setY(yPosition1);
		
		graphics.getPosition().add(coordinate5);
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(var.getName());
		nameLabel.setGraphics(graphicsName);
		
		HLAnnotation annotation = HlpngdefinitionFactory.eINSTANCE.createHLAnnotation();
		AnnotationGraphics labelPos = PnmlcoremodelFactory.eINSTANCE.createAnnotationGraphics();
		Coordinate offset = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		offset.setX(0);
		offset.setY(30);
		labelPos.setOffset(offset);
		annotation.setGraphics(labelPos);
		annotation.setText(var.getName());
		
		Variable variable = TermsFactory.eINSTANCE.createVariable();
		
		variable.setRefvariable(var);
		annotation.setStructure(variable);
		
		
		arc.setGraphics(graphics);
		arc.setId(id);
		arc.setName(nameLabel);
		arc.setSource(source);
		arc.setTarget(target);
		arc.setHlinscription(annotation);
		
		return arc;
		
	}
	
	public RefPlace createRefPlace(String id,
			int xPosition, int yPosition, String name, float nameX,
			float nameY, String parallelCardinality, PlaceNode node){
		
		RefPlace refPlace = AbstractginetFactory.eINSTANCE.createRefPlace();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//graphics
		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(columns.get(xPosition));
		coordinate1.setY(rows.get(yPosition));

		graphics.setPosition(coordinate1);
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		refPlace.setGraphics(graphics);
		refPlace.setName(nameLabel);
		refPlace.setId(id);
		refPlace.setParallelCardinality(parallelCardinality);
		refPlace.setRef(node);
		
		return refPlace;		
	}
	
	public RefPlace createRefPlace(String id,
			float xPosition, float yPosition, String name, float nameX,
			float nameY, String parallelCardinality, PlaceNode node){
		
		RefPlace refPlace = AbstractginetFactory.eINSTANCE.createRefPlace();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//graphics
		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(xPosition);
		coordinate1.setY(yPosition);

		graphics.setPosition(coordinate1);
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		refPlace.setGraphics(graphics);
		refPlace.setName(nameLabel);
		refPlace.setId(id);
		refPlace.setParallelCardinality(parallelCardinality);
		refPlace.setRef(node);
		
		return refPlace;		
	}
	
	public static Page createPage(String id,
			int xPosition, int yPosition, String name, float nameX,
			float nameY){
		
		Page page = AbstractginetFactory.eINSTANCE.createPage();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//graphics
		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(columns.get(xPosition));
		coordinate1.setY(rows.get(yPosition));

		graphics.setPosition(coordinate1);
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		page.setGraphics(graphics);
		page.setName(nameLabel);
		page.setId(id);
		
		return page;		
	}
	
	public static Page createPage(String id,
			float xPosition, float yPosition, String name, float nameX,
			float nameY){
		
		Page page = AbstractginetFactory.eINSTANCE.createPage();
		
		PnmlcoremodelFactory pnmlcoremodelFactory = PnmlcoremodelFactory.eINSTANCE;
		
		//graphics
		Coordinate coordinate = pnmlcoremodelFactory.createCoordinate();
		
		coordinate.setX(40);
		coordinate.setY(40);
		
		NodeGraphics graphics = pnmlcoremodelFactory.createNodeGraphics();

		graphics.setDimension(coordinate);
		
		Coordinate coordinate1 = pnmlcoremodelFactory.createCoordinate();

		coordinate1.setX(xPosition);
		coordinate1.setY(yPosition);

		graphics.setPosition(coordinate1);
		
		//Name		
		AnnotationGraphics graphicsName = pnmlcoremodelFactory
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = pnmlcoremodelFactory.createCoordinate();
		
		coordinate2.setX(nameX);
		coordinate2.setY(nameY);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText(name);
		nameLabel.setGraphics(graphicsName);
		
		page.setGraphics(graphics);
		page.setName(nameLabel);
		page.setId(id);
		
		return page;		
	}
	
	public void createInteractionProtocol(
			EList<InteractionProtocolElement> elements, int pos,
			Page pageReturn, PlaceNode placeIn, PlaceNode placeOut, EList<Declaration> ginetDeclarations, Place termination) {
		EList<Page> pages = new BasicEList<Page>();
		
		boolean isTermination = false;
		int hasTermination = 0;
		
		for (InteractionProtocolElement element : elements) {
			if(element instanceof Termination){
				isTermination = true;
				hasTermination++;
			}
		}

		if (elements.size() > 0) {
			Transition transition = null;
			int i = 0;
			for (InteractionProtocolElement element : elements) {
				if(!(element instanceof Termination)){
					int k = i + 1;
					RefPlace refPlace1 = null;
					Place place = null;
	
					RefPlace refPlace2 = null;
					Place place2 = null;
	
					Arc arc = null;
					Arc arc1 = null;
	
					Page page = null;
	
					if (i == 0) {
						refPlace1 = this.createRefPlace("InIP" + k,
								(80 + (320 * (i))), (float) 114.5, "InIP" + k, -4,
								-9, "1", placeIn);
					} else {
						place = this.createPlace("InIP" + k, (80 + (320 * (i))),
								(float) 114.5, "InIP" + k, -4, -9,
								this.getVarDec("gi"), 6, 21, true, "1");
						if (transition != null) {
							arc = this.createArc("", this.getVarDec("gi"), -9, -19,
									transition, place);
						}
	
					}
					Place placeTermination = null;
					Transition transTermination = null;
					Arc arcTermination = null;
					Arc arcTermination1 = null;
					if ((i + 1) < elements.size()- hasTermination) {
						place2 = this.createPlace("OutIP" + k, (240 + (320 * (i))),
								(float) 114.5, "OutIP" + k, -4, -9,
								this.getVarDec("gi"), 6, 21, false, "1");
						transition = this.createTransition("IPTransition" + k,
								(320 + (320 * i)), (float) 114.5, "IPTransition" + k,
								-9, -4, "1", "1", null);
						arc1 = this.createArc("", this.getVarDec("gi"), -9, -19,
								place2, transition);
					} else {
						refPlace2 = this.createRefPlace("OutIP" + k,
								(240 + (320 * (i))), (float) 114.5, "OutIP" + k, -4,
								-9, "1", placeOut);
						
						if(isTermination){
							this.setPosition(termination, (380 + (320 * (i))), (float) 114.5);
							placeTermination = termination;

							transTermination = this.createTransition("TTermination", (310 + (320 * (i))), (float) 114.5,"TTermination" ,-9, -4, "1", "1", null);
							
							arcTermination = this.createArc("", this.getVarDec("gi"), -9, -19,
									refPlace2, transTermination);
							
							arcTermination1 = this.createArc("", this.getVarDec("gi"), -9, -19,
									transTermination, termination);
						}
						transition = null;
					}
	
					if (place != null) {
						if (place2 != null) {
							page = this.getPageForElement(
									this.tree.getNode(elements.get(i)), this.tree,
									k + 1, place, place2, ginetDeclarations, termination);
						} else {
							page = this.getPageForElement(
									this.tree.getNode(elements.get(i)), this.tree,
									k + 1, place, refPlace2, ginetDeclarations, termination);
						}
					} else {
						if (place2 != null) {
							page = this.getPageForElement(
									this.tree.getNode(elements.get(i)), this.tree,
									k + 1, refPlace1, place2, ginetDeclarations, termination);
						} else {
							page = this.getPageForElement(
									this.tree.getNode(elements.get(i)), this.tree,
									k + 1, refPlace1, refPlace2, ginetDeclarations, termination);
						}
					}
	
					if (page != null) {
	
						NodeGraphics graphics = PnmlcoremodelFactory.eINSTANCE
								.createNodeGraphics();
	
						Coordinate coordinate = PnmlcoremodelFactory.eINSTANCE
								.createCoordinate();
	
						coordinate.setX(40);
						coordinate.setY(40);
	
						graphics.setDimension(coordinate);
	
						Coordinate coordinate1 = PnmlcoremodelFactory.eINSTANCE
								.createCoordinate();
	
						coordinate1.setX((160 + (320 * i)));
						coordinate1.setY((float) 114.5);
	
						graphics.setPosition(coordinate1);
	
						page.setGraphics(graphics);
	
						pages.add(page);
	
						if (place != null) {
							pageReturn.getObject().add(place);
						}
						if (refPlace1 != null) {
							pageReturn.getObject().add(refPlace1);
						}
						if (refPlace2 != null) {
							pageReturn.getObject().add(refPlace2);
						}
						if (arc != null) {
							pageReturn.getObject().add(arc);
						}
						if (arc1 != null) {
							pageReturn.getObject().add(arc1);
						}
						if (place2 != null) {
							pageReturn.getObject().add(place2);
						}
						if (transition != null) {
							pageReturn.getObject().add(transition);
						}
						if (placeTermination != null) {
							pageReturn.getObject().add(placeTermination);
						}
						if (arcTermination != null) {
							pageReturn.getObject().add(arcTermination);
						}
						if (transTermination != null) {
							pageReturn.getObject().add(transTermination);
						}
						if (arcTermination1 != null) {
							pageReturn.getObject().add(arcTermination1);
						}
						i++;
					}
					
				}else{
					if(elements.size() == 1 && isTermination){
						this.setPosition(termination, 100, (float) 114.5);
						Place placeTermination = termination;
						pageReturn.getObject().add(placeTermination);						
					}
				}
				pageReturn.getObject().addAll(pages);
			}
		} else {
			Page page = this.getPageForElement(
					this.tree.getNode(elements.get(0)), this.tree, pos,
					placeIn, placeOut, ginetDeclarations, termination);
			pageReturn.getObject().addAll(page.getArcs());
			pageReturn.getObject().addAll(page.getPlaces());
			pageReturn.getObject().addAll(page.getTransitions());
			pageReturn.getObject().addAll(page.getRefPlaces());
			pageReturn.getObject().addAll(page.getPages());
		}
	}
	
	private void setPosition(Place termination, float x, float y) {
		NodeGraphics graphics = PnmlcoremodelFactory.eINSTANCE
				.createNodeGraphics();

		Coordinate coordinate = PnmlcoremodelFactory.eINSTANCE
				.createCoordinate();

		coordinate.setX(40);
		coordinate.setY(40);

		graphics.setDimension(coordinate);

		Coordinate coordinate1 = PnmlcoremodelFactory.eINSTANCE
				.createCoordinate();

		coordinate1.setX(x);
		coordinate1.setY(y);
		
		graphics.setPosition(coordinate1);
		
		termination.setGraphics(graphics);		
	}

	protected void fillVarDeclAndNameSort(EList<Declaration> ginetDeclarations){
		EList<org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Declaration> declarations = ginetDeclarations.get(0).getStructure().getDeclaration();
		for (org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Declaration declaration : declarations) {
			if(declaration instanceof NamedSort){
				this.namedSorts.add((NamedSort) declaration);
			}
			if(declaration instanceof VariableDecl){
				this.variableDecls.add((VariableDecl) declaration);
			}
		}
	}
	
	protected EList<VariableDecl> fillVarDecl(EList<Declaration> ginetDeclarations){
		EList<org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Declaration> declarations = ginetDeclarations.get(0).getStructure().getDeclaration();
		EList<VariableDecl> varDecl = new BasicEList<VariableDecl>();
		for (org.pnml.tools.epnk.pntypes.hlpngs.datatypes.terms.Declaration declaration : declarations) {
			if(declaration instanceof VariableDecl){
				varDecl.add((VariableDecl) declaration);
			}
		}
		
		return varDecl;
	}
	
	protected VariableDecl getVarDec(String name){
		VariableDecl varReturn = TermsFactory.eINSTANCE.createVariableDecl();
		
		for (VariableDecl var : this.variableDecls) {
			if(var.getName().equals(name)){
				varReturn.setId(var.getId());
				varReturn.setName(var.getName());
				MultiSetSort multiSet = (MultiSetSort) var.getSort().clone();
				UserSort userSort = (UserSort) ((MultiSetSort) var.getSort()).getBasis().clone();
				userSort.setName(((UserSort)((MultiSetSort) var.getSort()).getBasis()).getName());
				userSort.setDeclaration(((UserSort)((MultiSetSort) var.getSort()).getBasis()).getDeclaration());
				multiSet.setBasis(userSort);
				varReturn.setSort(multiSet);
				return varReturn;
			}
		}
		return null;
	}
	
	protected VariableDecl getVarDec(String name, EList<VariableDecl> variableDecls){
		VariableDecl varReturn = TermsFactory.eINSTANCE.createVariableDecl();
		
		for (VariableDecl var : variableDecls) {
			if(var.getName().equals(name)){
				varReturn.setId(var.getId());
				varReturn.setName(var.getName());
				MultiSetSort multiSet = (MultiSetSort) var.getSort().clone();
				UserSort userSort = (UserSort) ((MultiSetSort) var.getSort()).getBasis().clone();
				userSort.setName(((UserSort)((MultiSetSort) var.getSort()).getBasis()).getName());
				userSort.setDeclaration(((UserSort)((MultiSetSort) var.getSort()).getBasis()).getDeclaration());
				multiSet.setBasis(userSort);
				varReturn.setSort(multiSet);
				return varReturn;
			}
		}
		return null;
	}
	
	protected NamedSort getNamedSort(String name){
		NamedSort sortReturn = TermsFactory.eINSTANCE.createNamedSort();
		
		for (NamedSort n : this.namedSorts) {
			if(n.getName().equals(name)){
				sortReturn.setId(n.getId());
				sortReturn.setName(n.getName());
				Sort def = n.getDef().clone();
				sortReturn.setDef(def);
				return sortReturn;
			}
		}
		return null;
	}
	
	public Page getPageForElement(NodeStructure element, TreeInterface tree, int count, PlaceNode placeIn, PlaceNode placeOut, EList<Declaration> ginetDeclarations, Place termination){
		return null;
	}
	
	public void createPageStructure(NodeStructure node, TreeInterface tree, PlaceNode placeIn, PlaceNode placeOut, EList<Declaration> ginetDeclarations, Place termination){
		
	}
}
